﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using Models;
using BLL;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Configuration;
using System.Text.RegularExpressions;

namespace WeChat
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class WxHandler : IHttpHandler
    {
        string rtn = "0";
        
        public void ProcessRequest(HttpContext context)
        {
            foreach (var param in context.Request.Form)
            {
                if (param == null || "".Equals(param.ToString()))
                {
                    rtn = "-1";//传递参数验证失败
                    return;
                }
            }        
            if (rtn != "-1")
            {
                SetBasicInfoBLL sb = new SetBasicInfoBLL();
                string funcName = context.Request.Form["funcName"];
                switch (funcName)
                {
                    case "getWechatBaseInfo":
                        try
                        {
                            JObject jo = new JObject();
                            jo.Add("baseInfo",JsonConvert.SerializeObject(sb.getWechatBaseInfo()));
                            Regex regex = new Regex(@"(?:\w+\.)*?(\w*\.(?:com\.cn|cn|com|net))[\\\/]*");

#pragma warning disable CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
                            string domainName = ConfigurationSettings.AppSettings["domainName"].ToString();
#pragma warning restore CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
                            if (!regex.IsMatch(domainName))
                            {
                                domainName = "请在配置文件中配置该项";
                            }
                            jo.Add("domainName", domainName);
                            rtn = jo.ToString();
                        }
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
                        catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
                        {
                            
                        }
                        break;
                    case "setWechatBaseInfo":
                        WechatBaseInfo wb = new WechatBaseInfo();
                        wb.token = context.Request.Form["token"];
                        wb.EncodingAESKey = context.Request.Form["EncodingAESKey"];
                        wb.appId = context.Request.Form["appId"];
                        wb.appSecret = context.Request.Form["appSecret"];
                        if (sb.setWechatBaseInfo(wb) > 0)
                        {
                            rtn = "1";                          
                        }
                        break;
                    case "getWxPayInfo":
                        rtn = JsonConvert.SerializeObject(WxPayAddBLL.getWxPayInfo());
                        break;

                    case "setWxPayInfo":
                        WxPayInfo wi = new WxPayInfo();
                        wi.companyName = context.Request.Form["companyName"];
                        wi.body = context.Request.Form["body"];
                        wi.product_id = context.Request.Form["product_id"];
                        wi.merchantNum = context.Request.Form["merchantNum"];
                        wi.payKey = context.Request.Form["payKey"];
                        if (WxPayAddBLL.setWxPayInfo(wi) > 0)
                        {
                            rtn = "1";
                        }

                        break;
                    case "getAliPayInfo":
                        rtn = JsonConvert.SerializeObject(WxPayAddBLL.getAliPayInfo());
                        break;
                    case "setAliPayInfo":
                        AlipayInfo ai = new AlipayInfo();
                        ai.alipay_public_key = context.Request.Form["alipay_public_key"];
                        ai.merchant_private_key = context.Request.Form["merchant_private_key"];
                        ai.merchant_public_key = context.Request.Form["merchant_public_key"];
                        ai.appId = context.Request.Form["appId"];
                        ai.pid = context.Request.Form["pid"];
                        if (WxPayAddBLL.setAliPayInfo(ai) > 0)
                        {
                            rtn = "1";
                        }
                        break;
                    default:
                        break;
                }
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write(rtn);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
